/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.CuaHangDAO;
import dao.LoaiNhanVienDAO;
import dao.NhanVienDAO;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Cuahang;
import pojo.Loainv;
import pojo.Nhanvien;


/**
 *
 * @author sangnguyen
 */
public class JInternalFrameNhanVien extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrameNhanVien
     */
    private DefaultTableModel tbModel;
    private DefaultComboBoxModel cmbModelLoaiNV;
    private DefaultComboBoxModel cmbModelCH;
    private List<Nhanvien> ls;
    private Vector vtMaLoaiNV;
    private Vector vtTenLoaiNV;
    private Vector vtMaLoaiCH;
    private Vector vtTenCH;
    public void XoaTrang(){
        this.jTextFieldHoTen.setText("");
        this.jTextAreaDiaChi.setText("");
        this.jTextFieldUsername.setText("");
        this.jFormattedTextFieldNgaySinh.setText("");
        this.jFormattedTextFieldDienThoai.setText("");
        this.jPasswordFieldPassword.setText("");
        this.jRadioButtonNam.setSelected(true);
        this.jRadioButtonNu.setSelected(false);
    }
    public JInternalFrameNhanVien() {
        initComponents();
        this.tbModel = new DefaultTableModel(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; 
        }
        };
        DateFormat formatDatetoString = new SimpleDateFormat("dd-MM-yyyy");
        this.tbModel.addColumn("STT");
        //this.tbModel.addColumn("ID");
        this.tbModel.addColumn("Họ tên nhân viên");                
        this.tbModel.addColumn("Ngày sinh");                
        this.tbModel.addColumn("Địa chỉ");                
        this.tbModel.addColumn("Điện thoại");                
        this.tbModel.addColumn("Giới tính");  
        this.tbModel.addColumn("Username");  
        this.tbModel.addColumn("Ngày tạo");  
        this.tbModel.addColumn("Loại nhân viên");  
        this.tbModel.addColumn("Cửa hàng");  
        ls = NhanVienDAO.SelectAllNhanvien();
        
        for(int i=0;i<ls.size();i++)
        {
            
            Vector vt = new Vector();            
            vt.add(i+1);
            vt.add(ls.get(i).getHoten());
            vt.add(formatDatetoString.format(ls.get(i).getNgaysinh()));
            vt.add(ls.get(i).getDiachi());
            vt.add(ls.get(i).getDienthoai());
            String gioiTinh="";
             if(ls.get(i).getGioitinh()==0){                
                gioiTinh ="Nam";
            }
                
            else
            {                
                gioiTinh ="Nu";
                
            }
            vt.add(gioiTinh);
            vt.add(ls.get(i).getUsername());
            vt.add(ls.get(i).getNgaytao());
            vt.add(ls.get(i).getLoainv().getLoai());  
            
            vt.add(ls.get(i).getCuahang().getTench());
            tbModel.addRow(vt);
        }        
        this.jTableNhanVien.setModel(tbModel);
        //loai nhan vien
        List<Loainv> dsLoaiNV = LoaiNhanVienDAO.SelectAllLoainv();        
        vtMaLoaiNV = new Vector();
        vtTenLoaiNV = new Vector();
        for(int i=0;i<dsLoaiNV.size();i++){
            
            vtMaLoaiNV.add(dsLoaiNV.get(i).getIdloai());            
            vtTenLoaiNV.add(dsLoaiNV.get(i).getLoai());
            
        }                
        cmbModelLoaiNV = new DefaultComboBoxModel(vtTenLoaiNV);
        this.jComboBoxLoaiNV.setModel(cmbModelLoaiNV);
        //cua hang
        vtMaLoaiCH = new Vector();
        vtTenCH = new Vector();
        List<Cuahang> dsCH= CuaHangDAO.SelectAllCuaHang();
        for(int i=0;i<dsCH.size();i++){
            vtMaLoaiCH.add(dsCH.get(i).getIdch());
            vtTenCH.add(dsCH.get(i).getTench());
        }
        cmbModelCH = new DefaultComboBoxModel(vtTenCH);
        this.jComboBoxCuaHang.setModel(cmbModelCH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jRadioButtonNam = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jRadioButtonNu = new javax.swing.JRadioButton();
        jFormattedTextFieldNgaySinh = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDiaChi = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jComboBoxLoaiNV = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextFieldHoTen = new javax.swing.JTextField();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        jComboBoxCuaHang = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableNhanVien = new javax.swing.JTable();
        jButtonThoat = new javax.swing.JButton();
        jButtonXoaTrang = new javax.swing.JButton();
        jButtonXoa = new javax.swing.JButton();
        jButtonThem = new javax.swing.JButton();
        jButtonCapNhat = new javax.swing.JButton();
        jFormattedTextFieldDienThoai = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();

        setClosable(true);
        setMaximumSize(new java.awt.Dimension(700, 750));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Nhân Viên");

        jLabel2.setText("Họ tên nhân viên");

        jLabel3.setText("Ngày sinh");

        jLabel4.setText("Địa chỉ");

        jRadioButtonNam.setSelected(true);
        jRadioButtonNam.setText("Nam");
        jRadioButtonNam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNamActionPerformed(evt);
            }
        });

        jLabel5.setText("Giới tính");

        jRadioButtonNu.setText("Nữ");
        jRadioButtonNu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNuActionPerformed(evt);
            }
        });

        jFormattedTextFieldNgaySinh.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd-MM-yyyy"))));

        jTextAreaDiaChi.setColumns(10);
        jTextAreaDiaChi.setRows(2);
        jScrollPane1.setViewportView(jTextAreaDiaChi);

        jLabel6.setText("Điện thoại");

        jTextFieldUsername.setPreferredSize(new java.awt.Dimension(111, 20));

        jLabel7.setText("Loại nhân viên");

        jLabel8.setText("Username");

        jLabel9.setText("Password");

        jLabel10.setText("Cửa hàng");

        jTableNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableNhanVienMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableNhanVien);

        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        jButtonXoaTrang.setText("Xóa trắng");
        jButtonXoaTrang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaTrangActionPerformed(evt);
            }
        });

        jButtonXoa.setText("Xóa");
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        jButtonThem.setText("Thêm");
        jButtonThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThemActionPerformed(evt);
            }
        });

        jButtonCapNhat.setText("Cập nhật");
        jButtonCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCapNhatActionPerformed(evt);
            }
        });

        jLabel11.setText("(dd-MM-yyyy)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jFormattedTextFieldNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel11))
                            .addComponent(jTextFieldHoTen)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBoxLoaiNV, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jComboBoxCuaHang, 0, 192, Short.MAX_VALUE)
                                            .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(10, 10, 10)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jRadioButtonNam)
                                                .addGap(18, 18, 18)
                                                .addComponent(jRadioButtonNu))
                                            .addComponent(jFormattedTextFieldDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(jButtonThem, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                        .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonXoaTrang, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(105, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(315, 315, 315))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jRadioButtonNam)
                    .addComponent(jRadioButtonNu)
                    .addComponent(jFormattedTextFieldNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jFormattedTextFieldDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxLoaiNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldUsername, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel8))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jPasswordFieldPassword)
                        .addComponent(jLabel9)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonThoat)
                    .addComponent(jButtonXoaTrang)
                    .addComponent(jButtonXoa)
                    .addComponent(jButtonThem)
                    .addComponent(jButtonCapNhat))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jRadioButtonNamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNamActionPerformed
        // TODO add your handling code here:
        this.jRadioButtonNu.setSelected(false);
    }//GEN-LAST:event_jRadioButtonNamActionPerformed

    private void jRadioButtonNuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNuActionPerformed
        // TODO add your handling code here:
        this.jRadioButtonNam.setSelected(false);
    }//GEN-LAST:event_jRadioButtonNuActionPerformed

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jButtonXoaTrangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaTrangActionPerformed
        // TODO add your handling code here:
        this.XoaTrang();
        
    }//GEN-LAST:event_jButtonXoaTrangActionPerformed

    private void jButtonThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThemActionPerformed
        // TODO add your handling code here:        
         if(this.jTextFieldHoTen.getText().isEmpty()==true || this.jTextAreaDiaChi.getText().isEmpty()==true || this.jTextFieldUsername.getText().isEmpty()==true|| this.jPasswordFieldPassword.getText().isEmpty()==true){
            JOptionPane.showMessageDialog(this, "Chưa nhập đầy đủ thông tin");
            return;
        }
        else
        {
            DateFormat formatDatetoString = new SimpleDateFormat("dd-MM-yyyy");
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            
            Nhanvien obj = new Nhanvien();
            obj.setHoten(this.jTextFieldHoTen.getText());
            obj.setDiachi(this.jTextAreaDiaChi.getText());
            
            obj.setDienthoai(this.jFormattedTextFieldDienThoai.getText());
            if(this.jTextFieldUsername.getText().trim().length()>6)            
                    obj.setUsername(this.jTextFieldUsername.getText().trim());
            else{
                JOptionPane.showMessageDialog(this, "Username không được ít hơn 6 ký tự!");    
                return;
            }
            
            if(this.jPasswordFieldPassword.getText().trim().length()>6)            
                    obj.setPassword(this.jPasswordFieldPassword.getText().trim());
            else{
                JOptionPane.showMessageDialog(this, "Password không được ít hơn 6 ký tự!");    
                return;
            }
            Calendar cal = Calendar.getInstance();            
            Date day = cal.getTime();
            obj.setNgaytao(day);
            String ngayHienTai = formatter.format(day);
            obj.setTinhtrang(1);
            String gioiTinh = "";
            if(this.jRadioButtonNam.isSelected()==true){
                obj.setGioitinh(0);
                gioiTinh ="Nam";
            }
                
            else
            {
                obj.setGioitinh(1);
                gioiTinh ="Nu";
                
            }                                        
         //   String s = formatter.format(this.jFormattedTextFieldNgaySinh.getValue());                        
            String s="";
            Date d = null; //= new Date();
            try {
                d = formatDatetoString.parse(this.jFormattedTextFieldNgaySinh.getText());                                                
                //System.out.println(this.jFormattedTextFieldNgaySinh.getText());
            } catch (ParseException ex) {
                Logger.getLogger(JInternalFrameNhanVien.class.getName()).log(Level.SEVERE, null, ex);
            }         
            int nYear = day.getYear() - d.getYear();
            if(nYear<18){
                JOptionPane.showMessageDialog(this, "Ngày sinh không hợp lệ (chưa đủ 18 tuổi)");
                return;
            }
            obj.setNgaysinh(d);
            int indexLoai= this.jComboBoxLoaiNV.getSelectedIndex();
            int iMaLoai = (Integer)this.vtMaLoaiNV.get(indexLoai);
            Loainv objLoaiNV = LoaiNhanVienDAO.GetLoainvID(iMaLoai);
            obj.setLoainv(objLoaiNV);
            int indexCH = this.jComboBoxCuaHang.getSelectedIndex();
            int iMaCH = (Integer)this.vtMaLoaiCH.get(indexCH);
            Cuahang objCuahang = CuaHangDAO.GetCuahangID(iMaCH);
            obj.setCuahang(objCuahang);            
            if(NhanVienDAO.InsertNhanvien(obj))
            {    
                JOptionPane.showMessageDialog(this, "Thêm thành công!");                                    
                Vector vt = new Vector();            
                vt.add(tbModel.getRowCount()+1);
                vt.add(obj.getHoten());
                vt.add(formatDatetoString.format(d));
                vt.add(obj.getDiachi());
                vt.add(obj.getDienthoai());
                vt.add(gioiTinh);
                vt.add(obj.getUsername());
                vt.add(ngayHienTai);
                vt.add(obj.getLoainv().getLoai());              
                vt.add(obj.getCuahang().getTench());
                tbModel.addRow(vt);            
                ls.add(obj);
                this.jTableNhanVien.setModel(tbModel);
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Thêm thất bại!");                                    
            }
        }
    }//GEN-LAST:event_jButtonThemActionPerformed

    private void jButtonCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCapNhatActionPerformed
        // TODO add your handling code here:
        try {
            String gioiTinh ="";
            // TODO add your handling code here:
            int confirm = JOptionPane.showConfirmDialog(this, "Bạn có muốn cập nhật thông tin nhân viên này không?", "Xác nhận", JOptionPane.YES_NO_OPTION);
            if(confirm == JOptionPane.YES_OPTION){
                int dong = this.jTableNhanVien.getSelectedRow();            
                Nhanvien obj = ls.get(dong);
                obj.setHoten(this.jTextFieldHoTen.getText());
                obj.setDiachi(this.jTextAreaDiaChi.getText());        
                obj.setDienthoai(this.jFormattedTextFieldDienThoai.getText());
                if(this.jTextFieldUsername.getText().trim().length()>6)            
                    obj.setUsername(this.jTextFieldUsername.getText().trim());
                else{
                    JOptionPane.showMessageDialog(this, "Username không được ít hơn 6 ký tự!");    
                    return;
                }

                if(this.jPasswordFieldPassword.getText().trim().length()>6)            
                        obj.setPassword(this.jPasswordFieldPassword.getText().trim());
                else{
                    JOptionPane.showMessageDialog(this, "Password không được ít hơn 6 ký tự!");    
                    return;
                }
                if(this.jRadioButtonNam.isSelected())
                {
                    obj.setGioitinh(1);
                    gioiTinh = "Nam";
                }
                else
                {
                    obj.setGioitinh(0);
                    gioiTinh="Nu";                            
                }
                obj.setTinhtrang(1);
                //ngay sinh
                SimpleDateFormat formatDate = new SimpleDateFormat("dd-MM-yyyy");                
                Date d = new Date();
                Date currentDate = null;
                String strCurDay ="";
                try {
                    d = (Date)formatDate.parse(this.jFormattedTextFieldNgaySinh.getText());                                
                    strCurDay = formatDate.format(Calendar.getInstance().getTime());                    
                    currentDate = (Date)formatDate.parse(strCurDay);                    
                } catch (ParseException ex) {
                    Logger.getLogger(JInternalFrameNhanVien.class.getName()).log(Level.SEVERE, null, ex);
                }         
                int nYear = currentDate.getYear() - d.getYear();
                if(nYear<18){
                    JOptionPane.showMessageDialog(this, "Ngày sinh không hợp lệ (chưa đủ 18 tuổi)");
                    return;
                }
                obj.setNgaysinh(d);
                int indexLoai= this.jComboBoxLoaiNV.getSelectedIndex();
                int iMaLoai = (Integer)this.vtMaLoaiNV.get(indexLoai);
                Loainv objLoaiNV = LoaiNhanVienDAO.GetLoainvID(iMaLoai);
                obj.setLoainv(objLoaiNV);
                int indexCH = this.jComboBoxCuaHang.getSelectedIndex();
                int iMaCH = (Integer)this.vtMaLoaiCH.get(indexCH);
                Cuahang objCuahang = CuaHangDAO.GetCuahangID(iMaCH);
                obj.setCuahang(objCuahang);            
                if(NhanVienDAO.UpdateNhanvien(obj)){
                    JOptionPane.showMessageDialog(this, "Cập nhật thành công!");                                    
                //Date dt = new Date(day.getYear()+1900, day.getMonth()+1, day.getDate());
                    tbModel.setValueAt(obj.getHoten(), dong, 1);
                    tbModel.setValueAt(formatDate.format(d), dong, 2);
                    tbModel.setValueAt(obj.getDiachi(), dong, 3);
                    tbModel.setValueAt(obj.getDienthoai(), dong, 4);                                
                    tbModel.setValueAt(obj.getUsername(), dong, 6);
                    tbModel.setValueAt(gioiTinh, dong, 5);
                    //vt.add(ls.get(i).getNgaytao());
                    tbModel.setValueAt(obj.getLoainv().getLoai(),dong,8);              
                    tbModel.setValueAt(obj.getCuahang().getTench(),dong,9);
                }
            }   
            else
            {
                JOptionPane.showMessageDialog(this, "Cập nhật thất bại!");                                    
                return;
            }
         }        
         catch (Exception ex){
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(JInternalFrameQuyDinh.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }//GEN-LAST:event_jButtonCapNhatActionPerformed

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            int confirm = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa nhân viên này không?", "Xác nhận", JOptionPane.YES_NO_OPTION);
            if(confirm == JOptionPane.YES_OPTION){
                int dong = this.jTableNhanVien.getSelectedRow();            
                Nhanvien obj = ls.get(dong);
               // obj.setHoten(this.jTextFieldHoTen.getText());
         //       obj.setDiachi(this.jTextAreaDiaChi.getText());        
         //       obj.setDienthoai(this.jFormattedTextFieldDienThoai.getText());                        
                obj.setTinhtrang(0);
                if(NhanVienDAO.DeleteNhanvien(obj)){
                    JOptionPane.showMessageDialog(this, "Đã xóa!"); 
                    tbModel.removeRow(dong);            
                    ls.remove(dong);
                    for(int i = 0;i<this.tbModel.getRowCount();i++)
                    {                
                        this.tbModel.setValueAt(i+1, i, 0);
                    }
                    this.jTableNhanVien.setModel(tbModel);
                }
                else{
                    JOptionPane.showMessageDialog(this, "Không xóa được!"); 
                    return;
                }
            
            }
            else
            {                
                return;
            }
        } catch (Exception ex){
            try {
                throw ex;
            } catch (Exception ex1) {
                Logger.getLogger(JInternalFrameQuyDinh.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        this.XoaTrang();
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void jTableNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableNhanVienMouseClicked
        // TODO add your handling code here:
        this.XoaTrang();
        try{                   
            int dong = this.jTableNhanVien.getSelectedRow();
            if(dong>=0){                            
                if(this.jTableNhanVien.getValueAt(dong,1).toString().isEmpty()==false){                    
                    this.jTextFieldHoTen.setText(this.jTableNhanVien.getValueAt(dong,1).toString());
                }
                
                if(this.jTableNhanVien.getValueAt(dong,3).toString().isEmpty()==false){                    
                    this.jTextAreaDiaChi.setText(this.jTableNhanVien.getValueAt(dong,3).toString());
                }
                     
                if(this.jTableNhanVien.getValueAt(dong,4).toString().isEmpty()==false){                    
                    this.jFormattedTextFieldDienThoai.setText(this.jTableNhanVien.getValueAt(dong,4).toString());
                }        
                if(this.jTableNhanVien.getValueAt(dong,6).toString().isEmpty()==false){                    
                    this.jTextFieldUsername.setText(this.jTableNhanVien.getValueAt(dong,6).toString());
                    Nhanvien nv = NhanVienDAO.GetNhanvienUsername(this.jTableNhanVien.getValueAt(dong,6).toString());
                    this.jPasswordFieldPassword.setText(nv.getPassword());
                }                                
                if(this.jTableNhanVien.getValueAt(dong,5).toString().isEmpty()==false){                    
                    if(this.jTableNhanVien.getValueAt(dong,5).toString()=="Nam")
                    {
                        this.jRadioButtonNam.setSelected(true);
                        this.jRadioButtonNu.setSelected(false);
                    }
                    else{
                        this.jRadioButtonNu.setSelected(true);
                        this.jRadioButtonNam.setSelected(false);
                    }
                        
                }                                
                if(this.jTableNhanVien.getValueAt(dong,8).toString().isEmpty()==false){                    
                    
                    this.jComboBoxLoaiNV.setSelectedItem(this.jTableNhanVien.getValueAt(dong,8).toString());
                }  
                if(this.jTableNhanVien.getValueAt(dong,9).toString().isEmpty()==false){                    
                    
                    this.jComboBoxCuaHang.setSelectedItem(this.jTableNhanVien.getValueAt(dong,9).toString());
                }
                Date ngaySinh = null;
                SimpleDateFormat formatDate = new SimpleDateFormat("dd-MM-yyyy");                
                ngaySinh = formatDate.parse(this.jTableNhanVien.getValueAt(dong,2).toString());                                        
                this.jFormattedTextFieldNgaySinh.setValue(ngaySinh);
            }
        }
        catch (Exception ex){
            //throw ex;
        }
    }//GEN-LAST:event_jTableNhanVienMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCapNhat;
    private javax.swing.JButton jButtonThem;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JButton jButtonXoa;
    private javax.swing.JButton jButtonXoaTrang;
    private javax.swing.JComboBox jComboBoxCuaHang;
    private javax.swing.JComboBox jComboBoxLoaiNV;
    private javax.swing.JFormattedTextField jFormattedTextFieldDienThoai;
    private javax.swing.JFormattedTextField jFormattedTextFieldNgaySinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JRadioButton jRadioButtonNam;
    private javax.swing.JRadioButton jRadioButtonNu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableNhanVien;
    private javax.swing.JTextArea jTextAreaDiaChi;
    private javax.swing.JTextField jTextFieldHoTen;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
}
